//
//  NSDictionary+ACCommon.m
//  AcmedcareCore
//
//  Created by AcZx on 2018/3/9.
//

#import "NSDictionary+ACCommon.h"
#import <objc/runtime.h>

@implementation NSDictionary (ACCommon)

/**
 * 字典转json字符串
 */
- (NSString *)changeToJsonStr{
    if (self == nil) return nil;
    if (self.allKeys.count == 0) return @"";
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    return jsonString;
}

- (NSMutableDictionary *)dictionaryNonullValue{
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (int i = 0; i < count; ++ i) {
        NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
        if ([self respondsToSelector:NSSelectorFromString(key)]) {
            id value = [self valueForKey:key];
            dict[key] = value ? value : @"";
        }
    }
    return dict;
}

@end
